<template>
  <div>

    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入案件名称" suffix-icon="el-icon-search" v-model="caseName"></el-input>
      <el-input style="width: 200px" placeholder="请输入案号" suffix-icon="el-icon-message" class="ml-5" v-model="num"></el-input>
      <el-input style="width: 200px" placeholder="请输入承办人" suffix-icon="el-icon-position" class="ml-5" v-model="undertaker"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-popconfirm
          class="ml-5"
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="您确定批量删除这些数据吗？"
          @confirm="delBatch"
      >
        <el-button type="danger" slot="reference">取消收藏 <i class="el-icon-star-off"></i></el-button>
      </el-popconfirm>
    </div>

    <el-table :data="tableData" border stripe :header-cell-class-name="headerBg" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="num" label="案号" width="250"></el-table-column>
      <el-table-column prop="caseName" label="案例名称" width="400"></el-table-column>
      <el-table-column prop="undertaker" label="承办人"></el-table-column>
      <el-table-column label="操作"  width="200" align="center">
        <template v-slot="scope">
        <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="del(scope.row)"
          >
            <el-button type="danger" slot="reference">取消收藏<i class="el-icon-delete"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "CaseView",
  data(){
    return {
      userid: JSON.parse(localStorage.getItem('user')).id,
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      caseName: "",
      num:"",
      undertaker:"",
      form: {},
      dialogFormVisible: false,
      multipleSelection: [],
      headerBg: 'headerBg'
    }
  },
  created() {
    // 请求分页查询数据
    this.load()
  },
  methods:{
    load() {
      this.request.get("/collection/getCollection",{
        params:{
          userId: this.userid,
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          caseName: this.caseName,
          num: this.num,
          undertaker: this.undertaker
        }
      }).then(res => {
        console.log('加载')
        console.log(res)
        this.tableData = res.data.list
        this.total = res.data.total
      })
    },
    save (){
      this.request.post("/user",this.form).then(res => {
        if (res.code === '200'){
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()
        }else {
          this.$message.error("保存失败")
        }
      })
    },
    handleAdd (){
      this.dialogFormVisible = true
      this.form = {}
    },
    del (row){
      this.request.get("/collection/deleteInfo",{params:{userid:this.userid,caseid:row.caseId}}).then(res => {
        if (res.code === '200'){
          this.$message.success("取消成功")
          this.load()
        }else {
          this.$message.error("取消失败")
        }
      })
    },
    handleSelectionChange (val){
      console.log(val)
      this.multipleSelection = val
    },
    delBatch (){
      let ids = this.multipleSelection.map(v => v.id) //[{},{}] => [1,2,3]
      this.request.post("/user/del/batch",ids).then(res => {
        if (res.code === '200'){
          this.$message.success("批量删除成功")
          this.load()
        }else {
          this.$message.error("批量删除失败")
        }
      })
    },
    reset (){
      this.username = ""
      this.email = ""
      this.address = ""
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },
    exp() {
      window.open("http://localhost:8080/user/export")
    }
  }

}
</script>

<style>
.headerBg {
  background: #eee!important;
}
</style>
